package com.huishu.commons.pojo.vo;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

@Data
public class ExceptionEmailVO implements Serializable {

    private String serverName;

    private String errorMsg;

    private String params;

    private String api;

    private String profile;

    public String getParams() {
        return params;
    }

    public void setParams(Map requestParams) {
        try {
            this.params = JSONObject.toJSONString(getMapObjByRequestParas(requestParams));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public Map<String, String> getMapObjByRequestParas(Map requestParams) throws UnsupportedEncodingException {
        Map<String, String> reqMap = new HashMap<>();
        for (Object o : requestParams.keySet()) {
            String name = (String) o;
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            reqMap.put(name, URLDecoder.decode(valueStr, "UTF-8"));
        }
        return reqMap;
    }
}
